{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from numpy.random import RandomState"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 定义神经网络的参数，输入和输出节点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 8\n",
    "dataset_size = 128\n",
    "w1= tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1), trainable=True)\n",
    "w2= tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1), trainable=True)\n",
    "x = tf.placeholder(tf.float32, shape=(None, 2), name=\"x-input\")\n",
    "y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 定义前向传播过程，损失函数及反向传播算法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = tf.matmul(x, w1)\n",
    "y = tf.matmul(a, w2)\n",
    "cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0))) \n",
    "train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  3. 生成模拟数据集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "rdm = RandomState(1)\n",
    "X = rdm.rand(dataset_size, 2)\n",
    "Y = [[int(x1+x2 < 1)] for (x1, x2) in X]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 创建一个会话来运行TensorFlow程序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w1: [[-0.81131822  1.48459876  0.06532937]\n",
      " [-2.4427042   0.0992484   0.59122431]]\n",
      "w2: [[-0.81131822]\n",
      " [ 1.48459876]\n",
      " [ 0.06532937]]\n",
      "\n",
      "\n",
      "After 0 training step(s), cross entropy on all data is 0.0674925\n",
      "w1: [[-0.81231821  1.4855988   0.06632921]\n",
      " [-2.44370413  0.1002484   0.59222424]]\n",
      "w2: [[-0.81231821]\n",
      " [ 1.4855988 ]\n",
      " [ 0.06632937]]\n",
      "After 1000 training step(s), cross entropy on all data is 0.0163385\n",
      "w1: [[-1.27549374  1.93239319  0.71818316]\n",
      " [-2.82764411  0.47066161  1.14189851]]\n",
      "w2: [[-1.21198606]\n",
      " [ 1.95956361]\n",
      " [ 0.55081969]]\n",
      "After 2000 training step(s), cross entropy on all data is 0.00907547\n",
      "w1: [[-1.51397622  2.1591146   1.07429051]\n",
      " [-3.01708913  0.64845419  1.46364999]]\n",
      "w2: [[-1.40506411]\n",
      " [ 2.20634365]\n",
      " [ 0.83948904]]\n",
      "After 3000 training step(s), cross entropy on all data is 0.00714436\n",
      "w1: [[-1.65394425  2.29218411  1.27439237]\n",
      " [-3.14156055  0.76467752  1.66820383]]\n",
      "w2: [[-1.52613485]\n",
      " [ 2.35394239]\n",
      " [ 1.01985705]]\n",
      "After 4000 training step(s), cross entropy on all data is 0.00578471\n",
      "w1: [[-1.79143536  2.42184758  1.46388769]\n",
      " [-3.28938985  0.90241849  1.88527477]]\n",
      "w2: [[-1.66073918]\n",
      " [ 2.50406837]\n",
      " [ 1.20711744]]\n",
      "After 5000 training step(s), cross entropy on all data is 0.00430222\n",
      "w1: [[-1.96215367  2.58266187  1.68243814]\n",
      " [-3.46851015  1.07014132  2.11830711]]\n",
      "w2: [[-1.82502723]\n",
      " [ 2.68580961]\n",
      " [ 1.41858089]]\n",
      "After 6000 training step(s), cross entropy on all data is 0.00280812\n",
      "w1: [[-2.15782928  2.76797581  1.91360271]\n",
      " [-3.66963673  1.26025426  2.35586643]]\n",
      "w2: [[-2.01201868]\n",
      " [ 2.88948369]\n",
      " [ 1.64122665]]\n",
      "After 7000 training step(s), cross entropy on all data is 0.00137464\n",
      "w1: [[-2.3685751   2.96918178  2.14771676]\n",
      " [-3.88335228  1.46428025  2.59262657]]\n",
      "w2: [[-2.21356416]\n",
      " [ 3.10541439]\n",
      " [ 1.86731029]]\n",
      "After 8000 training step(s), cross entropy on all data is 2.11566e-05\n",
      "w1: [[-2.58881521  3.18109441  2.38287973]\n",
      " [-4.10505152  1.67768049  2.82888079]]\n",
      "w2: [[-2.42511773]\n",
      " [ 3.32894397]\n",
      " [ 2.09544468]]\n",
      "After 9000 training step(s), cross entropy on all data is -0\n",
      "w1: [[-2.59392238  3.18602753  2.38825655]\n",
      " [-4.1101799   1.68263638  2.83427358]]\n",
      "w2: [[-2.4300375 ]\n",
      " [ 3.33411145]\n",
      " [ 2.10067439]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    init_op = tf.global_variables_initializer()\n",
    "    sess.run(init_op)\n",
    "    \n",
    "    # 输出目前（未经训练）的参数取值。\n",
    "    print(\"w1:\", sess.run(w1))\n",
    "    print(\"w2:\", sess.run(w2))\n",
    "    print(\"\\n\")\n",
    "    \n",
    "    # 训练模型。\n",
    "    STEPS = 10000\n",
    "    for i in range(STEPS):\n",
    "        start = (i*batch_size) % dataset_size\n",
    "        end = (i*batch_size) % dataset_size + batch_size\n",
    "        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})\n",
    "        if i % 1000 == 0:\n",
    "            total_cross_entropy = sess.run(cross_entropy, feed_dict={x: X, y_: Y})\n",
    "            print(\"After %d training step(s), cross entropy on all data is %g\" % (i, total_cross_entropy))\n",
    "            print(\"w1:\", sess.run(w1))\n",
    "            print(\"w2:\", sess.run(w2))    \n",
    "    # 输出训练后的参数取值。\n",
    "#     print(\"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "oldHeight": 305,
   "position": {
    "height": "333px",
    "left": "711px",
    "right": "20px",
    "top": "127px",
    "width": "521px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "varInspector_section_display": "block",
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
